package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.ApartmentInfoService;
import com.atguigu.lease.web.admin.service.GraphInfoService;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    @Autowired
    private ApartmentFeeValueMapper apartmentFeeValueMapper;

    @Autowired
    private ApartmentLabelMapper apartmentLabelMapper;
    @Autowired
    private ApartmentFacilityMapper apartmentFacilityMapper;

    @Autowired
    private GraphInfoMapper graphInfoMapper;

    @Autowired
    private ProvinceInfoMapper provinceInfoMapper;

    @Autowired
    private CityInfoMapper cityInfoMapper;

    @Autowired
    private DistrictInfoMapper districtInfoMapper;

    @Autowired
    private FacilityInfoMapper facilityInfoMapper;

    @Autowired
    private FeeValueMapper feeValueMapper;

    @Autowired
    private LabelInfoMapper labelInfoMapper;




    @Transactional
    @Override
    public void saveOrUpdateApartment(ApartmentSubmitVo apartmentSubmitVo) {
        Long apartmentId  = apartmentSubmitVo.getId();// 有就是更新，没有就是保存
        // 封装区域地名
        Long provinceId = apartmentSubmitVo.getProvinceId();
        Long cityId = apartmentSubmitVo.getCityId();
        Long districtId = apartmentSubmitVo.getDistrictId();
        String provinceName = provinceInfoMapper.selectById(provinceId).getName();
        String cityName = cityInfoMapper.selectById(cityId).getName();
        String districtName = districtInfoMapper.selectById(districtId).getName();
        apartmentSubmitVo.setProvinceName(provinceName);
        apartmentSubmitVo.setCityName(cityName);
        apartmentSubmitVo.setDistrictName(districtName);

        this.saveOrUpdate(apartmentSubmitVo);

        // 需要填入的数据
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();


        if (apartmentId != null){
            // 更新
            // 删除标签中间表
            QueryWrapper<ApartmentLabel> apartmentLabelQueryWrapper = new QueryWrapper<>();
            apartmentLabelQueryWrapper.eq("apartment_id",apartmentId);
            apartmentLabelMapper.delete(apartmentLabelQueryWrapper);

            // 删除配套中间表
            QueryWrapper<ApartmentFacility> apartmentFacilityQueryWrapper = new QueryWrapper<>();
            apartmentFacilityQueryWrapper.eq("apartment_id",apartmentId);
            apartmentFacilityMapper.delete(apartmentFacilityQueryWrapper);

            // 删除杂费中间表
            QueryWrapper<ApartmentFeeValue> apartmentFeeValueQueryWrapper = new QueryWrapper<>();
            apartmentFeeValueQueryWrapper.eq("apartment_id",apartmentId);
            apartmentFeeValueMapper.delete(apartmentFeeValueQueryWrapper);

            // 删除图片表
            QueryWrapper<GraphInfo> graphInfoQueryWrapper = new QueryWrapper<>();
            graphInfoQueryWrapper.eq("item_type", ItemType.APARTMENT);
            graphInfoQueryWrapper.eq("item_id",apartmentId);
            graphInfoMapper.delete(graphInfoQueryWrapper);



        }

        // 插入新数据
         apartmentId = apartmentSubmitVo.getId();// 避免新增的时候第一次获取id为null
        // 标签中间表
        apartmentLabelMapper.insertBatch(apartmentId,labelIds);

        // 配套中间表
        apartmentFacilityMapper.insertBatch(apartmentId,facilityInfoIds);

        // 杂费中间表
        apartmentFeeValueMapper.insertBatch(apartmentId,feeValueIds);

        // 图片表
        graphInfoMapper.insertBatch(1,apartmentId,graphVoList);

    }

    @Override
    public IPage<ApartmentItemVo> pageItem(IPage<ApartmentItemVo> itemVoIPage, ApartmentQueryVo queryVo) {
        IPage<ApartmentItemVo> iPage = baseMapper.pageItem(itemVoIPage,queryVo);
        return iPage;
    }

    @Override
    public ApartmentDetailVo getApartmentDetailById(Long id) {
        ApartmentInfo apartmentInfo = baseMapper.selectById(id);

        // 图片信息
        QueryWrapper<GraphInfo> graphInfoQueryWrapper = new QueryWrapper<>();
        graphInfoQueryWrapper.eq("item_type",ItemType.APARTMENT);
        graphInfoQueryWrapper.eq("item_id",id);
        List<GraphInfo> graphInfos = graphInfoMapper.selectList(graphInfoQueryWrapper);
        List<GraphVo> graphVos = graphInfos.stream().map(graphInfo -> {
            GraphVo graphVo = new GraphVo();
            BeanUtils.copyProperties(graphInfo,graphVo);
            return graphVo;
        }).collect(Collectors.toList());

//        List<GraphVo> graphVos = new ArrayList<>();
//        for (GraphInfo graphInfo : graphInfos){
//            GraphVo graphVo = new GraphVo();
//            BeanUtils.copyProperties(graphInfo,graphVo);
//            graphVos.add(graphVo);
//        }

        // 配套
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectListByApartmentId(id);

        // 标签
        List<LabelInfo> labelInfoList = labelInfoMapper.selectListByApartmentId(id);

        // 杂费
        List<FeeValueVo> feeValueVOs = feeValueMapper.selectListByApartmentId(id);


        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        BeanUtils.copyProperties(apartmentInfo,apartmentDetailVo);
        apartmentDetailVo.setGraphVoList(graphVos);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        apartmentDetailVo.setFeeValueVoList(feeValueVOs);

        return apartmentDetailVo;
    }
    @Transactional
    @Override
    public void removeApartmentById(Long id) {

        // 删除标签中间表
        QueryWrapper<ApartmentLabel> apartmentLabelQueryWrapper = new QueryWrapper<>();
        apartmentLabelQueryWrapper.eq("apartment_id",id);
        apartmentLabelMapper.delete(apartmentLabelQueryWrapper);

        // 删除配套中间表
        QueryWrapper<ApartmentFacility> apartmentFacilityQueryWrapper = new QueryWrapper<>();
        apartmentFacilityQueryWrapper.eq("apartment_id",id);
        apartmentFacilityMapper.delete(apartmentFacilityQueryWrapper);

        // 删除杂费中间表
        QueryWrapper<ApartmentFeeValue> apartmentFeeValueQueryWrapper = new QueryWrapper<>();
        apartmentFeeValueQueryWrapper.eq("apartment_id",id);
        apartmentFeeValueMapper.delete(apartmentFeeValueQueryWrapper);

        // 删除图片表
        QueryWrapper<GraphInfo> graphInfoQueryWrapper = new QueryWrapper<>();
        graphInfoQueryWrapper.eq("item_type", ItemType.APARTMENT);
        graphInfoQueryWrapper.eq("item_id",id);
        graphInfoMapper.delete(graphInfoQueryWrapper);

        //删除公寓
        super.removeById(id);

    }


}




